package com.android.kmlmrg.views;

import java.util.Calendar;

import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.DatePicker;
import android.widget.EditText;

import com.android.kmlmrg.R;
import com.android.kmlmrg.R.menu;
import com.android.kmlmrg.model.User;
import com.android.kmlmrg.utils.Const;
import com.android.kmlmrg.utils.MenuBarHelper;


public class AddOrUpdateContact extends Activity implements Const {

	/**LOG**/
	private static final  String LOG_TAG = AddOrUpdateContact.class.getName();

	private User user = null ;
	
	private boolean isForAdd ;	
	
	private EditText  editTextbirthday;
	 static final int DATE_DIALOG_ID = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(LOG_TAG, "START - onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_contact);
		user = (User) getIntent().getSerializableExtra("user");
		isForAdd =((Boolean) getIntent().getSerializableExtra("isForAdd"));

	    
		editTextbirthday = (EditText) findViewById(R.id.addContactBirthday);

		editTextbirthday.setOnTouchListener(new OnTouchListener(){ 

			public boolean onTouch(View v, MotionEvent event) {
				 if(v == editTextbirthday)
	                    showDialog(DATE_DIALOG_ID);
	                return false; 
			}
        });
		
		Log.i(LOG_TAG, "user - " + user);
		if ( user != null ) {
		populateView(user);
		}else{
			user = new User();
		}
		Log.i(LOG_TAG, "END - onCreate");
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
        Calendar c = Calendar.getInstance();
        int cyear = c.get(Calendar.YEAR);
        int cmonth = c.get(Calendar.MONTH);
        int cday = c.get(Calendar.DAY_OF_MONTH);
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
        }
        return null;
    }
	
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			   String dd = String.valueOf(dayOfMonth);
			   if(dd.length()==1){
				   dd= "0"+dd;
			   }
			   String mm = String.valueOf(monthOfYear+1);
			   if(mm.length()==1){
				   mm= "0"+mm;
			   }
			   String date_selected = dd+"/"+mm+"/"+String.valueOf(year);
			   editTextbirthday.setText(date_selected);
			
		}
    };
    
	@Override
	public boolean onCreateOptionsMenu(Menu pMenu) {
		Log.i(LOG_TAG, "START - onCreateOptionsMenu(Menu pMenu)");
		getMenuInflater().inflate (menu.add_contact, pMenu);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			ActionBar actionBar = getActionBar();
			actionBar.setDisplayHomeAsUpEnabled(true);
		}
		Log.i(LOG_TAG, "END - onCreateOptionsMenu(Menu pMenu)");
		return super.onCreateOptionsMenu(pMenu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		return MenuBarHelper.manageMenuItemClick(item, this);
	}


	public User instanciateUser(){
		Log.i(LOG_TAG, "START - instanciateUser()");

		EditText editTextnom = (EditText) findViewById(R.id.addContactName);
		String nom = editTextnom.getText().toString();
		Log.i(LOG_TAG, "NOM : " + nom );

		EditText editTextprenom = (EditText) findViewById(R.id.addContactLastName);
		String prenom = editTextprenom.getText().toString();
		Log.i(LOG_TAG, "PRENOM : " + prenom );

		editTextbirthday = (EditText) findViewById(R.id.addContactBirthday);
		String birthday = editTextbirthday.getText().toString();
		Log.i(LOG_TAG, "BIRTHDAY : " + birthday );

		EditText editTextStreetNumber = (EditText) findViewById(R.id.addContacStreetNumber);
		String streetNumber = editTextStreetNumber.getText().toString();
		Log.i(LOG_TAG, "STREET NUMBER : " + streetNumber );

		EditText editTextAdress = (EditText) findViewById(R.id.addContacAdress);
		String adress = editTextAdress.getText().toString();
		Log.i(LOG_TAG, "ADRESS : " + adress );

		EditText editTextCity = (EditText) findViewById(R.id.addContacCity);
		String city = editTextCity.getText().toString();
		Log.i(LOG_TAG, "CITY : " + city );

		EditText editTextZipCode = (EditText) findViewById(R.id.addContacZipCode);
		String zipCode = editTextZipCode.getText().toString();
		Log.i(LOG_TAG, "ZIPCODE : " + zipCode );

		EditText editTextPhoneNumber = (EditText) findViewById(R.id.addContacPhoneNumber);
		String phoneNumber = editTextPhoneNumber.getText().toString();
		Log.i(LOG_TAG, "PHONE NUMBER : " + phoneNumber );


		EditText editTextMail = (EditText) findViewById(R.id.addContacAdressMail);
		String mail = editTextMail.getText().toString();
		Log.i(LOG_TAG, "MAIL : " + mail );

		EditText editTextCountry = (EditText) findViewById(R.id.addContacCountry);
		String country =  editTextCountry.getText().toString();
		Log.i(LOG_TAG, "country : " + country );



		user.setNom(nom);
		user.setPrenom(prenom);
		user.setDateNaissance(birthday);
		user.setNumeroRue(streetNumber);
		user.setNomRue(adress);
		user.setVille(city);
		user.setCodePostal(zipCode);
		user.setTel(phoneNumber);
		user.setEmail(mail);
		user.setPays(country);


		Log.i(LOG_TAG, "END - instanciateUser()");
		return user;
	}

	private void populateView(User user){
		EditText editTextnom = (EditText) findViewById(R.id.addContactName);
		editTextnom.setText(user.getNom() == null ? "" : user.getNom());

		EditText editTextprenom = (EditText) findViewById(R.id.addContactLastName);
		editTextprenom.setText(user.getPrenom() == null ? "" : user.getPrenom());

		editTextbirthday.setText(user.getDateNaissance() == null ? "" : user.getDateNaissance());

		EditText editTextStreetNumber = (EditText) findViewById(R.id.addContacStreetNumber);
		editTextStreetNumber.setText(user.getNumeroRue() == null ? "" : user.getNumeroRue());

		EditText editTextAdress = (EditText) findViewById(R.id.addContacAdress);
		editTextAdress.setText(user.getNomRue() == null ? "" : user.getNomRue());

		EditText editTextCity = (EditText) findViewById(R.id.addContacCity);
		editTextCity.setText(user.getVille() == null ? "" : user.getVille() );

		EditText editTextZipCode = (EditText) findViewById(R.id.addContacZipCode);
		editTextZipCode.setText(user.getCodePostal() == null ? "" : user.getCodePostal());		

		EditText editTextPhoneNumber = (EditText) findViewById(R.id.addContacPhoneNumber);
		editTextPhoneNumber.setText(user.getTel() == null ? "" : user.getTel());


		EditText editTextMail = (EditText) findViewById(R.id.addContacAdressMail);
		editTextMail.setText(user.getEmail() == null ? "" : user.getEmail());

		EditText editTextCountry = (EditText) findViewById(R.id.addContacCountry);
		editTextCountry.setText(user.getPays() == null ? "" : user.getPays());
		

	}

	public boolean isForAdd() {
		return isForAdd;
	}

	public void setForAdd(boolean isForAdd) {
		this.isForAdd = isForAdd;
	}



	public void updateProfil(){
		Intent data = new Intent();
		data.putExtra("user", user);
		setResult(RESULT_OK, data);
		finish();
	}

	public boolean validation() {
		boolean valid = true;
		EditText editTextnom = (EditText) findViewById(R.id.addContactName);
		if(user.getNom().isEmpty()){
			editTextnom.setError("empty");
			valid = false;
		}else{
			editTextnom.setError(null);
		}
		EditText editTextprenom = (EditText) findViewById(R.id.addContactLastName);
		if(user.getPrenom().isEmpty()){
			editTextprenom.setError("empty");
			valid = false;
		}else{
			editTextprenom.setError(null);
		}

		EditText editTextStreetNumber = (EditText) findViewById(R.id.addContacStreetNumber);
		if(user.getNumeroRue().isEmpty()){
			editTextStreetNumber.setError("invalid");
			valid = false;
		}else{
			editTextStreetNumber.setError(null);
		}
		
		EditText editTextAdress = (EditText) findViewById(R.id.addContacAdress);
		if(user.getNomRue().isEmpty()){
			editTextAdress.setError("empty");
			valid = false;
		}else{
			editTextAdress.setError(null);
		}
		
		EditText editTextCity = (EditText) findViewById(R.id.addContacCity);
		if(user.getVille().isEmpty()){
			editTextCity.setError("empty");
			valid = false;
		}else{
			editTextCity.setError(null);
		}
		
		EditText editTextZipCode = (EditText) findViewById(R.id.addContacZipCode);
		if(user.getCodePostal().isEmpty()){	
			editTextZipCode.setError("invalid");
			valid = false;
		}else{
			editTextZipCode.setError(null);
		}
		
		EditText editTextPhoneNumber = (EditText) findViewById(R.id.addContacPhoneNumber);
		if(user.getTel().isEmpty()||user.getTel().matches("[a-zA-Z]")){
			editTextPhoneNumber.setError("invalid");
			valid = false;
		}else{
			editTextPhoneNumber.setError(null);
		}	

		EditText editTextMail = (EditText) findViewById(R.id.addContacAdressMail);
		if(user.getEmail().isEmpty()){
			editTextMail.setError("empty");
			valid = false;
		}else{
			editTextMail.setError(null);
		}

		EditText editTextCountry = (EditText) findViewById(R.id.addContacCountry);
		if(user.getPays().isEmpty()){
			editTextCountry.setError("empty");
			valid = false;
		}else{
			editTextCountry.setError(null);
		}
		
		return valid;
	}

}
